//
//  KDNewsAnalysisController.m
//  WangJingApp
//
//  Created by 魏强 on 17/1/10.
//  Copyright © 2017年 KDTech. All rights reserved.
//  网情分析板块

#import "KDNewsAnalysisController.h"
#import "KDManageRequestTool.h"
@interface KDNewsAnalysisController ()<UITableViewDelegate, UITableViewDataSource,UIScrollViewDelegate>
/**  scrollView的宽度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollView_Cons_w;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**  Top10数据汇总 */
@property (nonatomic, strong) NSArray *infoArr;
/**  上一次的滑动位置 */
@property (nonatomic, assign) float lastContentOffset;
/**  tabBar原始y */
@property (nonatomic, assign) CGFloat originalTabY;
/**  <#Description#> */
@property (nonatomic, strong) NSDictionary *data;
/**  本周省域舆情分布情况webView */
@property (weak, nonatomic) IBOutlet UIWebView *weeklyNewsDistribWV;
/**  舆情信息分类webView */
@property (weak, nonatomic) IBOutlet UIWebView *newsClassifiedWV;
/**  舆情趋势webView */
@property (weak, nonatomic) IBOutlet UIWebView *newsTendencyWV;

@end

@implementation KDNewsAnalysisController
#pragma mark - 懒加载区
- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [[NSArray alloc]init] ;
    }
    return _infoArr;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.originalTabY = self.tabBarController.tabBar.y;
    // Do any additional setup after loading the view from its nib.
    [self loadWebData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.scrollView_Cons_w.constant = screenW;
}
#pragma mark - 板块布置区
- (void)setupTableView{
//    UINib *xib = [UINib nibWithNibName:@"KDUnitPerformanceCell" bundle:nil];
//    [self.tableView registerNib:xib forCellReuseIdentifier:@"KDUnitPerformanceCell"];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.tableView.footer = footer;
    [self.tableView.header beginRefreshing];
}
#pragma mark - 事件点击区

#pragma mark - 网络请求区
/**
 *  获取三个webView的url
 */
- (void)loadWebData{
    KDBaseParam *param = [[KDBaseParam alloc]init];
    param.method = @"calendar/webCircumstancesAnalyse";
    [KDManageRequestTool getWebAnalyseWithParam:param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.status isEqualToString:@"1"]) {
            KDLog(@"%@", responseObejct.data);
            self.data = responseObejct.data;
            [self loadWebView];
        }
    } failure:^(NSError *error) {
        KDLog(@"%@", error);
    }];
}
- (void)loadWebView{
    
    [self.weeklyNewsDistribWV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.data[@"line"]]]];
    [self.newsClassifiedWV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.data[@"scattergram"]]]];
    [self.newsTendencyWV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.data[@"pie"]]]];
}
- (void)loadData{
    self.infoArr = [self getFakeData];
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.header endRefreshing];
    });
}
- (void)loadMoreData{
    //    self.page.integerValue ++;
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.infoArr];
    [arr addObjectsFromArray:[self getFakeData]] ;
    self.infoArr = arr;
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.footer endRefreshing];
    });
    
}
- (NSArray *)getFakeData
{
    return @[@"1、东莞大朗出租屋火灾:死亡人数增至9人 14人被救出",@"2、为清除火灾隐患 东莞出租屋房中房等将被强制拆除",@"3、宝安沙井出租屋大火续：刑拘增至9人",@"4、深圳致7死出租屋火灾9人被刑拘",@"5、深圳出租屋火灾敲警钟 切莫贪便宜购买假冒伪劣产品",@" 6、深圳宝安一出租屋火灾已致7人遇难",@"7、疑电动车充电引火灾 深圳宝安一出租屋起火7死4重伤",@"8、深圳出租屋大火7人死，宝安区连夜责令电动车不得在...",@"9、东莞大朗出租屋失火致9死2伤",@"10、广东东莞大朗镇发生火灾致9死2伤"];
    
}
#pragma mark - 通知接收区

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId =@"newsCell";
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.infoArr[indexPath.row];
    return cell;
    
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        self.lastContentOffset = scrollView.contentOffset.y;
    }
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        NSInteger yOffset = scrollView.contentOffset.y;
        UITabBar *tb = self.tabBarController.tabBar;
        if (yOffset > 0) {//视情况隐藏tabar
            [UIView animateWithDuration:0.5 animations:^{
                tb.y = self.originalTabY + yOffset;
            }];
        }
        if (yOffset < self.lastContentOffset) {
            [UIView animateWithDuration:0.5 animations:^{
                tb.y = self.originalTabY;
            }];
        }
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
